layui.define(function (exports) { //配置后台端口
    var application = {};
    localStorage.removeItem("layuiAdmin");
    var $ = layui.jquery;
    var layer = layui.layer;
    var year_list = localStorage.getItem("year_list");
    if (year_list != null && year_list != undefined) { //统一时间规范
        $("#yearlist").html(year_list);
    }


    application.ip = function () {
        var host = document.location.hostname;
        var apiUrl = 'http://47.107.111.35:8096/hs'; //阿里云
        return apiUrl; //后台端口 上线前记得修改
    }
    application.tableChoose = function () {
        layui.use(['layer'], function () {
            var layer = layui.layer,
                $ = layui.$;
            $(document).on("click", ".layui-table-body table.layui-table tbody tr", function () {
                var index = $(this).attr('data-index');
                var tableBox = $(this).parents('.layui-table-box');
                var tableDiv;
                //存在固定列
                if (tableBox.find(".layui-table-fixed.layui-table-fixed-l").length > 0) {
                    tableDiv = tableBox.find(".layui-table-fixed.layui-table-fixed-l");
                } else {
                    tableDiv = tableBox.find(".layui-table-body.layui-table-main");
                }
                //获取已选中列并取消选中
                /* var trs = tableDiv.find(".layui-unselect.layui-form-checkbox.layui-form-checked").parent().parent().parent();
                 for (var i = 0; i < trs.length; i++) {
                     var ind = $(trs[i]).attr("data-index");
                     if (ind != index) {
                         var checkCell = tableDiv.find("tr[data-index=" + ind + "]").find("td div.laytable-cell-checkbox div.layui-form-checkbox I");
                         if (checkCell.length > 0) {
                             checkCell.click();
                         }
                     }
                 }*/
                //选中单击行
                var checkCell = tableDiv.find("tr[data-index=" + index + "]").find("td div.laytable-cell-checkbox div.layui-form-checkbox I");
                if (checkCell.length > 0) {
                    checkCell.click();
                }
            });
            $(document).on("click", "td div.laytable-cell-checkbox div.layui-form-checkbox", function (e) {
                e.stopPropagation();
            });

        });
    }


    application.httpGetRequest = function (url, data, async) {
        var index = layer.load(1, {
            shade: [0.1, '#fff'] //0.1透明度的白色背景
        });
        var defaultAsyncType = true;
        var requestData = "";
        defaultAsyncType = (async == null ? defaultAsyncType : async);
        var defer = $.Deferred();
        $.ajax({
            url: url,
            dataType: 'json',
            type: 'GET',
            async: defaultAsyncType,
            data: data,
            contentType: "application/json",
            xhrFields: {
                withCredentials: true
            },
            success: function (data) {
                if (data.code == 403) {
                    var host = document.location.hostname;
                   // console.log(host);
                    parent.window.location.href = '/hs/views/login.html';
                    return;
                }
                if (data.code != 200) {
                    parent.layer.alert('查询数据失败,' + data.msg + "!", {
                        icon: 2
                    });
                    layer.close(index);
                    return false;
                } else {
                    requestData = data.data;
                    defer.resolve(data)
                }
                layer.close(index);
            },
            error: function () {
                layer.msg('获取数据出错', {
                    offset: '15px',
                    icon: 2,
                    time: 1000
                });
                layer.close(index);
            }
        });
        //return requestData;20170331461B48611504
        return defer.promise();
    }

    application.httpPostRequest = function (url, data) {
        var index = layer.load(1, {
            shade: [0.1, '#fff'] //0.1透明度的白色背景
        });
        var requestData = "";
        $.ajax({
            url: url,
            dataType: 'json',
            type: 'POST',
            contentType: "application/json",
            data: data,
            xhrFields: {
                withCredentials: true
            },
            success: function (data) {
                if (data.code == 403) {
                    window.location.href = 'login.html';
                    return;

                }
                if (data.code == 200) {
                    parent.layer.alert('新增数据成功', {
                        icon: 1
                    }, function (index) {
                        var indexNum = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(indexNum); //再执行关闭
                        parent.location.reload(); // 父页面刷新
                    });
                } else {
                    parent.layer.alert('修改数据失败,' + data.msg + "!", {
                        icon: 2
                    });
                }
                layer.close(index);

            },
            error: function () {
                layer.msg('获取数据出错', {
                    offset: '15px',
                    icon: 2,
                    time: 1000
                });
                layer.close(index);
            }
        });
        return requestData;
    }


    application.httpPatchRequest = function (url, data) {
        var index = layer.load(1, {
            shade: [0.1, '#fff'] //0.1透明度的白色背景
        });
        var requestData = "";
        $.ajax({
            url: url,
            dataType: 'json',
            type: 'PATCH',
            contentType: "application/json",
            data: data,
            xhrFields: {
                withCredentials: true
            },
            success: function (data) {
                if (data.code == 403) {
                    parent.window.location.href = '/hs/views/login.html';
                    return;
                }
                if (data.code == 200) {
                    parent.layer.alert('修改数据成功', {
                        icon: 1
                    }, function (index) {
                        var indexNum = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(indexNum); //再执行关闭
                        parent.location.reload(); // 父页面刷新
                    });
                } else {
                    parent.layer.alert('修改数据失败,' + data.msg + "!", {
                        icon: 2
                    });
                }
                layer.close(index);

            },
            error: function () {
                layer.msg('获取数据出错', {
                    offset: '15px',
                    icon: 2,
                    time: 1000
                });
                layer.close(index);
            }
        });
        return requestData;
    }


    application.httpDeleteRequest = function (url, data) {
        var index = layer.load(1, {
            shade: [0.1, '#fff'] //0.1透明度的白色背景
        });
        var requestData = "";
        $.ajax({
            url: url,
            dataType: 'json',
            type: 'DELETE',
            contentType: "application/json",
            data: data,
            xhrFields: {
                withCredentials: true
            },
            success: function (data) {
                if (data.code == 403) {
                    parent.window.location.href = '/hs/views/login.html';
                    return;
                }
                if (data.code == 200) {
                    parent.layer.alert('删除数据成功', {
                        icon: 1
                    }, function (index) {
                        var indexNum = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(indexNum); //再执行关闭
                        parent.location.reload(); // 父页面刷新
                    });
                } else {
                    parent.layer.alert('删除数据失败,' + data.msg + "!", {
                        icon: 2
                    });
                }
                layer.close(index);

            },
            error: function () {
                layer.msg('删除数据出错', {
                    offset: '15px',
                    icon: 2,
                    time: 1000
                });
                layer.close(index);
            }
        });
        return requestData;
    }


    application.httpPatchRequestNoClose = function (url, data) {
        var index = layer.load(1, {
            shade: [0.1, '#fff'] //0.1透明度的白色背景
        });
        var defer = $.Deferred();
        $.ajax({
            url: url,
            dataType: 'json',
            type: 'PATCH',
            contentType: "application/json",
            data: data,
            xhrFields: {
                withCredentials: true
            },
            success: function (data) {
                if (data.code == 403) {
                    parent.window.location.href = '/hs/views/login.html';
                    return;

                }
                if (data.code == 200) {
                    var indexNum = parent.layer.alert('修改数据成功', {
                        icon: 1
                    }, function () {
                        parent.layer.close(indexNum); //再执行关闭
                        defer.resolve(data);
                    });
                } else {
                    parent.layer.alert('修改数据失败,' + data.msg + "!", {
                        icon: 2
                    });
                }
                layer.close(index);
            },
            error: function () {
                layer.msg('修改数据出错', {
                    offset: '15px',
                    icon: 2,
                    time: 1000
                });
                layer.close(index);
            }
        });
        return defer.promise();
    }


    application.httpPostRequestNoClose = function (url, data) {
        var index = layer.load(1, {
            shade: [0.1, '#fff'] //0.1透明度的白色背景
        });
        var defer = $.Deferred();
        $.ajax({
            url: url,
            dataType: 'json',
            type: 'Post',
            contentType: "application/json",
            data: data,
            xhrFields: {
                withCredentials: true
            },
            success: function (data) {
                if (data.code == 403) {
                    parent.window.location.href = '/hs/views/login.html';
                    return;
                }
                if (data.code == 200) {
                    layer.msg('新增数据成功', {
                        icon: 1
                    });
                    defer.resolve(data);
                    // var indexNum = parent.layer.alert('新增数据成功', { icon: 1 }, function() {
                    //     parent.layer.close(indexNum); //再执行关闭
                    //     defer.resolve(data);
                    // });
                } else {
                    parent.layer.alert('新增数据失败,' + data.msg + "!", {
                        icon: 2
                    });
                }
                layer.close(index);
            },
            error: function () {
                layer.msg('新增数据出错', {
                    offset: '15px',
                    icon: 2,
                    time: 1000
                });
                layer.close(index);
            }
        });
        return defer.promise();
    }

    application.httpDeleteRequestNoClose = function (url, data) {
        var index = layer.load(1, {
            shade: [0.1, '#fff'] //0.1透明度的白色背景
        });
        var defer = $.Deferred();
        $.ajax({
            url: url,
            dataType: 'json',
            type: 'DELETE',
            contentType: "application/json",
            data: data,
            xhrFields: {
                withCredentials: true
            },
            success: function (data) {
                if (data.code == 403) {
                    parent.window.location.href = '/hs/views/login.html';
                    return;
                }
                if (data.code == 200) {
                    layer.msg('删除数据成功', {
                        icon: 1
                    });
                    defer.resolve(data);
                    // var indexNum = parent.layer.alert('删除数据成功', { icon: 1 }, function() {
                    //     parent.layer.close(indexNum); //再执行关闭
                    //     defer.resolve(data);
                    // });
                } else {
                    parent.layer.alert('删除数据失败,' + data.msg + "!", {
                        icon: 2
                    });
                    defer.resolve(data);
                }
                layer.close(index);

            },
            error: function () {
                layer.msg('删除数据出错', {
                    offset: '15px',
                    icon: 2,
                    time: 1000
                });
                layer.close(index);
            }
        });
        return defer.promise();
    }

    application.checkLoginStatus = function (data) {
        if (data.code == 403) {
            parent.window.location.href = '/hs/views/login.html';
        }
    }

    application.toString = function () {
        return '';
    }

    exports('application', application);
});